using System;
using Microsoft.Practices.ObjectBuilder2;

namespace Microsoft.Practices.Unity.Interception
{
    public class InterceptionReflectionStrategy : BuilderStrategy
    {
        public override void PreBuildUp(IBuilderContext context)
        {
            Type typeToBuild;

            if (BuildKey.TryGetType(context.BuildKey, out typeToBuild))
            {
                Type originalType;

                if (!BuildKey.TryGetType(context.OriginalBuildKey, out originalType))
                    originalType = typeToBuild;

                IObjectFactory factory = context.Locator.Get<IObjectFactory>();

                if (factory != null)
                    InterceptionReflector.Reflect(originalType, typeToBuild, context.Policies, factory);
            }

            base.PreBuildUp(context);
        }
    }
}